<template>
  <div>
    <el-tabs type="border-card">
      <el-tab-pane label="WeChat配置">
        <r-search
          ref="search"
          :searchData="searchData"
          :searchForm="searchForm"
          :searchHandle="searchHandle"
          :inline="false"
          :searchRules="searchRules"
          labelWidth="150px"
          labelPosition="right"
        >
          <div slot="slot1">
            <el-upload
              class="upload-demo"
              :action="action"
              name="files"
              :limit="1"
              :on-success="on_success1"
              :on-exceed="handleExceed"
            >
              <el-button size="mini" type="primary">点击上传</el-button>
            </el-upload>
          </div>

          <div slot="slot2">
            <el-upload
              class="upload-demo"
              :action="action"
              name="files"
              :limit="1"
              :on-success="on_success2"
              :on-exceed="handleExceed"
            >
              <el-button size="mini" type="primary">点击上传</el-button>
            </el-upload>
          </div>
        </r-search>
      </el-tab-pane>
      <el-tab-pane label="Alipay配置">
        <r-search
          ref="search2"
          :searchData="searchData2"
          :searchForm="searchForm2"
          :searchHandle="searchHandle2"
          :inline="false"
          :searchRules="searchRules2"
          labelWidth="150px"
          labelPosition="right"
        />
      </el-tab-pane>
      <el-tab-pane label="短信配置">
        <r-search
          ref="search3"
          :searchData="searchData3"
          :searchForm="searchForm3"
          :searchHandle="searchHandle3"
          :inline="false"
          :searchRules="searchRules3"
          labelWidth="150px"
          labelPosition="right"
        />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
export default {
  name: "platform_information",
  data() {
    return {
      searchData: {
        wxAppId: "",
        wxMchName: "",
        wxMchId: "",
        wxMchKey: "",
        wxSecrect: "",
        wxCertPath: "",
        serviceId: "",
        msgTemplateId: "",
        wxPemkeyPath: "",
        wxSerialNo: ""
      },
      searchForm: [
        {
          type: "input",
          label: "WeChatAPPID",
          placeholder: "",
          prop: "wxAppId",
          width: 300
        },
        {
          type: "input",
          label: "WeChat商户Name",
          placeholder: "",
          prop: "wxMchName",
          width: 300
        },
        {
          type: "input",
          label: "WeChat商户ID",
          placeholder: "",
          prop: "wxMchId",
          width: 300
        },
        { type: "input", label: "WeChat商户KEY", prop: "wxMchKey", width: 300 },
        {
          type: "input",
          label: "WeChat商户密码",
          placeholder: "",
          prop: "wxSecrect",
          width: 300
        },
        {
          type: "input",
          label: "WeChat退款证书",
          placeholder: "",
          isDisabled: () => {
            return true;
          },
          prop: "wxCertPath",
          width: 400
        },
        { type: "slot", name: "slot1" },
        {
          type: "input",
          label: "Payment分ID",
          placeholder: "",
          prop: "serviceId",
          width: 300
        },
        {
          type: "input",
          label: "公众号消息TemplateID",
          placeholder: "",
          prop: "msgTemplateId",
          width: 400
        },
        {
          type: "input",
          label: "WeChat私钥证书",
          placeholder: "",
          isDisabled: () => {
            return true;
          },
          prop: "wxPemkeyPath",
          width: 400
        },
        { type: "slot", name: "slot2" },
        {
          type: "input",
          label: "商户API证书序列号",
          placeholder: "",
          prop: "wxSerialNo",
          width: 400
        }
      ],
      searchHandle: [
        { type: "primary", label: "Save", handle: e => this.elFormSubmit() }
      ],
      searchRules: {
        wxAppId: [{ required: true, message: "Please Enter", trigger: "blur" }],
        wxMchName: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        wxMchId: [{ required: true, message: "Please Enter", trigger: "blur" }],
        wxMchKey: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        wxSecrect: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        wxCertPath: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        serviceId: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        msgTemplateId: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        wxPemkeyPath: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        wxSerialNo: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ]
      },

      searchData2: {
        aliAppId: "",
        privateKey: "",
        publicKey: ""
      },
      searchForm2: [
        {
          type: "input",
          label: "AlipayAPPID",
          placeholder: "",
          prop: "aliAppId",
          width: 200
        },
        // { type: "input",label:'sellerID', placeholder: "", prop: "nickName", width: 200 },
        {
          type: "textarea",
          label: "私钥key",
          placeholder: "",
          prop: "privateKey",
          width: 200
        },
        { type: "textarea", label: "公钥key", prop: "publicKey", width: 200 }
      ],
      searchHandle2: [
        { type: "primary", label: "Save", handle: e => this.elFormSubmit2() }
      ],
      searchRules2: {
        aliAppId: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        privateKey: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        publicKey: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ]
      },

      searchData3: {
        smsCode: "",
        smsSignName: "",
        smsAccessKeySecret: "",

        smsCode2: "",
        smsCode3: "",
        smsCode4: "",
        msgTemplateId2: "",
        msgTemplateId3: "",
        msgTemplateId4: "",
        msgTemplateId5: "",
        msgTemplateId6: "",
        msgTemplateId7: "",
        msgTemplateId8: "",
        msgTemplateId9: ""
      },
      searchForm3: [
        {
          type: "input",
          label: "TemplateID",
          placeholder: "",
          prop: "smsCode",
          width: 200
        },
        {
          type: "input",
          label: "短信Username",
          placeholder: "",
          prop: "smsSignName",
          width: 200
        },
        {
          type: "input",
          label: "短信密码",
          placeholder: "",
          prop: "smsAccessKeySecret",
          width: 200
        },

        {
          type: "input",
          label: "Balance不足提醒短信Templatecode",
          placeholder: "",
          prop: "smsCode2",
          width: 200
        },
        {
          type: "input",
          label: "欠费提醒短信Templatecode",
          placeholder: "",
          prop: "smsCode3",
          width: 200
        },
        {
          type: "input",
          label: "Recharge通知短信Templatecode",
          placeholder: "",
          prop: "smsCode4",
          width: 200
        },
        {
          type: "input",
          label: "撤回通知",
          placeholder: "",
          prop: "msgTemplateId2",
          width: 200
        },
        {
          type: "input",
          label: "退款申请通知",
          placeholder: "",
          prop: "msgTemplateId3",
          width: 200
        },
        {
          type: "input",
          label: "账户欠费通知",
          placeholder: "",
          prop: "msgTemplateId4",
          width: 200
        },
        {
          type: "input",
          label: "RechargeSuccess通知",
          placeholder: "",
          prop: "msgTemplateId5",
          width: 200
        },
        {
          type: "input",
          label: "上门取货通知",
          placeholder: "",
          prop: "msgTemplateId6",
          width: 200
        },
        {
          type: "input",
          label: "Balance不足提醒",
          placeholder: "",
          prop: "msgTemplateId7",
          width: 200
        },
        {
          type: "input",
          label: "处理结果通知",
          placeholder: "",
          prop: "msgTemplateId8",
          width: 200
        },
        {
          type: "input",
          label: "RechargeSuccess通知",
          placeholder: "",
          prop: "msgTemplateId9",
          width: 200
        }
      ],
      searchHandle3: [
        { type: "primary", label: "Save", handle: e => this.elFormSubmit3() }
      ],
      searchRules3: {
        smsCode: [{ required: true, message: "Please Enter", trigger: "blur" }],
        smsSignName: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        smsAccessKeySecret: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],

        smsCode2: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        smsCode3: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        smsCode4: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        msgTemplateId2: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        msgTemplateId3: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        msgTemplateId4: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        msgTemplateId5: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        msgTemplateId6: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        msgTemplateId7: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        msgTemplateId8: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ],
        msgTemplateId9: [
          { required: true, message: "Please Enter", trigger: "blur" }
        ]
      },

      id: ""
    };
  },
  computed: {
    action() {
      return process.env.VUE_APP_URL + "api/file/uploadCert";
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    on_success1(res) {
      this.searchData.wxCertPath = res.data.imgPath;
    },
    on_success2(res) {
      this.searchData.wxPemkeyPath = res.data.imgPath;
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `Current限制选择 3 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      );
    },
    async getData() {
      var res = await this.apis.payDetails({});
      this.id = res.data.id;

      var data = res.data;

      for (var key in this.searchData) {
        for (var key2 in data) {
          if (key == key2) {
            this.searchData[key] = data[key2];
          }
        }
      }

      for (var key in this.searchData2) {
        for (var key2 in data) {
          if (key == key2) {
            this.searchData2[key] = data[key2];
          }
        }
      }

      for (var key in this.searchData3) {
        for (var key2 in data) {
          if (key == key2) {
            this.searchData3[key] = data[key2];
          }
        }
      }
    },
    elFormSubmit() {
      this.$refs.search.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.submit1();
        }
      });
    },
    elFormSubmit2() {
      this.$refs.search2.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.submit2();
        }
      });
    },
    elFormSubmit3() {
      this.$refs.search3.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.submit3();
        }
      });
    },
    async submit1() {
      if (this.id) {
        var res = await this.apis.pay_updata({
          id: this.id,
          wxMchName: this.searchData.wxMchName,
          wxMchId: this.searchData.wxMchId,
          wxAppId: this.searchData.wxAppId,
          wxMchKey: this.searchData.wxMchKey,
          wxSecrect: this.searchData.wxSecrect,
          wxCertPath: this.searchData.wxCertPath,
          serviceId: this.searchData.serviceId,
          msgTemplateId: this.searchData.msgTemplateId,
          wxPemkeyPath: this.searchData.wxPemkeyPath,
          wxSerialNo: this.searchData.wxSerialNo
        });
      } else {
        var res = await this.apis.pay_add({
          wxMchName: this.searchData.wxMchName,
          wxMchId: this.searchData.wxMchId,
          wxAppId: this.searchData.wxAppId,
          wxMchKey: this.searchData.wxMchKey,
          wxSecrect: this.searchData.wxSecrect,
          wxCertPath: this.searchData.wxCertPath,
          serviceId: this.searchData.serviceId,
          msgTemplateId: this.searchData.msgTemplateId,
          wxPemkeyPath: this.searchData.wxPemkeyPath,
          wxSerialNo: this.searchData.wxSerialNo
        });
      }

      this.$message({
        message: "Operations Success",
        type: "success"
      });
    },
    async submit2() {
      if (this.id) {
        var res = await this.apis.pay_updata({
          id: this.id,
          aliAppId: this.searchData2.aliAppId,
          privateKey: this.searchData2.privateKey,
          publicKey: this.searchData2.publicKey
        });
      } else {
        var res = await this.apis.pay_add({
          aliAppId: this.searchData2.aliAppId,
          privateKey: this.searchData2.privateKey,
          publicKey: this.searchData2.publicKey
        });
      }

      this.$message({
        message: "Operations Success",
        type: "success"
      });
    },
    async submit3() {
      if (this.id) {
        var res = await this.apis.pay_updata({
          id: this.id,
          smsCode: this.searchData3.smsCode,
          smsSignName: this.searchData3.smsSignName,
          smsAccessKeySecret: this.searchData3.smsAccessKeySecret,

          smsCode2: this.searchData3.smsCode2,
          smsCode3: this.searchData3.smsCode3,
          smsCode4: this.searchData3.smsCode4,
          msgTemplateId2: this.searchData3.msgTemplateId2,
          msgTemplateId3: this.searchData3.msgTemplateId3,
          msgTemplateId4: this.searchData3.msgTemplateId4,
          msgTemplateId5: this.searchData3.msgTemplateId5,
          msgTemplateId6: this.searchData3.msgTemplateId6,
          msgTemplateId7: this.searchData3.msgTemplateId7,
          msgTemplateId8: this.searchData3.msgTemplateId8,
          msgTemplateId9: this.searchData3.msgTemplateId9
        });
      } else {
        var res = await this.apis.pay_add({
          smsCode: this.searchData3.smsCode,
          smsSignName: this.searchData3.smsSignName,
          smsAccessKeySecret: this.searchData3.smsAccessKeySecret,

          smsCode2: this.searchData3.smsCode2,
          smsCode3: this.searchData3.smsCode3,
          smsCode4: this.searchData3.smsCode4,
          msgTemplateId2: this.searchData3.msgTemplateId2,
          msgTemplateId3: this.searchData3.msgTemplateId3,
          msgTemplateId4: this.searchData3.msgTemplateId4,
          msgTemplateId5: this.searchData3.msgTemplateId5,
          msgTemplateId6: this.searchData3.msgTemplateId6,
          msgTemplateId7: this.searchData3.msgTemplateId7,
          msgTemplateId8: this.searchData3.msgTemplateId8,
          msgTemplateId9: this.searchData3.msgTemplateId9
        });
      }

      this.$message({
        message: "Operations Success",
        type: "success"
      });
    }
  }
};
</script>

<style scoped lang="scss">
.c1 {
  > div {
    > div:nth-child(1) {
      line-height: 40px;
      font-weight: bold;
      font-size: 18px;
    }
  }
}
</style>
